package com.seed.web.servlet.init;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.seed.commons.dbutil.DBBean;
import com.seed.service.sysmanager.IMenuItem;
import com.seed.service.sysmanager.IMenuModel;
import com.seed.web.form.sysmanager.MenuItemForm;

/**
 * @标题：系统启动初始化
 * @描述：
 * @编写者：杨浩泉
 * @日期：2012-2-10<b>下午10:04:26</b>
 */
public class SystemInitServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	private Log log = LogFactory.getLog(getClass()) ;
	
	private WebApplicationContext ctx;

	@Override
	public void init() throws ServletException {
		log.info("开始初始化系统访问参数......") ;
		ServletContext servletContext = this.getServletContext();
		this.ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
		
		initMenuModel() ;
	}

	/**
	 * 初始化系统菜单模块
	 */
	public void initMenuModel() {
		log.info("初始化系统菜单模块......") ;
		IMenuModel imm = (IMenuModel) this.ctx.getBean("menuModelService") ;
		
		List<DBBean> dblist = imm.queryMenuModel() ;
		
		this.getServletContext().setAttribute("MenuModelJson", dblist) ;
		
		initMenuItem(dblist) ;
	}
	
	/**
	 * 初始化系统菜单列表
	 */
	public void initMenuItem(List<DBBean> dblist) {
		log.info("通过菜单模块来获取并初始化对应的菜单列表......") ;
		
		Map<String,List<MenuItemForm>> menuMap = new HashMap<String,List<MenuItemForm>>() ;
		
		IMenuItem imi = (IMenuItem) this.ctx.getBean("menuItemService") ;
		
		for(DBBean db : dblist) {
			String mmid = db.getbean("MMID") ;
			
			List<MenuItemForm> menuItemList = new ArrayList<MenuItemForm>() ;
			
			//加载一级菜单列表
			Map<String,Object[]> mapKeyRoot = new HashMap<String,Object[]>() ;
			mapKeyRoot.put("BYMenuItemRoot", new Object[]{mmid,"Y"}) ;
			List<DBBean> dblist1 = imi.queryMenuItem(mapKeyRoot) ;
			for(DBBean db1 : dblist1) {
				
				//System.out.println(db1.getbean("mlid") +"\t"+ db1.getbean("MLName") +"\t"+ db1.getbean("MLISROOT") +"\t"+ db1.getbean("MLUrl") +"\t"+ db1.getbean("MLDesc") +"\t"+ db1.getbean("MLIcon") +"\t"+ db1.getbean("addDate") +"\t"+ db1.getbean("SEED_MENU_MODEL_MMID_FK") +"\t"+ db1.getbean("SEED_MENU_ITEM_MLID_FK"));
				
				MenuItemForm mroot = new MenuItemForm() ;
				mroot.setMlId(db1.getbean("mlid").toString()) ;
				mroot.setMlName(db1.getbean("MLName").toString()) ;
				mroot.setMlIsRoot(db1.getbean("MLISROOT").toString()) ;
				mroot.setMlUrl((String) db1.getbean("MLUrl")) ;
				mroot.setMlDesc(db1.getbean("MLDesc").toString()) ;
				mroot.setMlIcon((String)db1.getbean("MLIcon")) ;
				mroot.setAddDate(db1.getbean("addDate").toString()) ;
				mroot.setMenuItemFk((String)db1.getbean("SEED_MENU_ITEM_MLId_FK")) ;
				mroot.setMenuModelFK((String)db1.getbean("SEED_MENU_MODEL_MMId_FK")) ;
				
				menuItemList.add(mroot) ;
				
				//加载二级菜单列表
				Map<String,Object[]> mapKeyChild = new HashMap<String,Object[]>() ;
				mapKeyChild.put("BYMenuItemFK", new Object[]{db1.getbean("mlid")}) ;
				List<DBBean> dbchild = imi.queryMenuItem(mapKeyChild) ;
				for(DBBean db2 : dbchild) {
					//System.out.println(db2.getbean("mlid") +"\t"+ db2.getbean("MLName") +"\t"+ db2.getbean("MLISROOT") +"\t"+ db2.getbean("SEED_MENU_MODEL_MMID_FK") +"\t"+ db2.getbean("SEED_MENU_ITEM_MLID_FK"));
					
					MenuItemForm mchild = new MenuItemForm() ;
					mchild.setMlId(db2.getbean("mlid").toString()) ;
					mchild.setMlName(db2.getbean("MLName").toString()) ;
					mchild.setMlIsRoot(db2.getbean("MLISROOT").toString()) ;
					mchild.setMlUrl((String)db2.getbean("MLUrl")) ;
					mchild.setMlDesc(db2.getbean("MLDesc").toString()) ;
					mchild.setMlIcon((String)db2.getbean("MLIcon")) ;
					mchild.setMlFunbar((String)db2.getbean("MLFunbar")) ;
					mchild.setAddDate(db2.getbean("addDate").toString()) ;
					mchild.setMenuItemFk((String)db2.getbean("SEED_MENU_ITEM_MLId_FK")) ;
					mchild.setMenuModelFK((String)db2.getbean("SEED_MENU_MODEL_MMId_FK")) ;
					
					menuItemList.add(mchild) ;
				}
			}
			menuMap.put(mmid, menuItemList) ;
		}
		this.getServletContext().setAttribute("menulist", menuMap) ;
	}
	

}
